import global from "./global.js"
import store from '../store/index.js'
const request = {
	get(url, data) {
		return new Promise((resolve, reject) => {
			// data.at_time = new Date().getTime() 
			uni.request({
				url: global.baseUrl + url,
				data: data,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
					"Authorization": uni.getStorageSync('TZ_token') ? 'bearer' + uni.getStorageSync(
						'TZ_token') : null
				},
				method: "GET",
				success: (response) => {
					if (response.data) {
						if (response.statusCode >= 200 && response.statusCode <= 400) {
							resolve(response.data)
						} else {
							reject(response.data)
							if ((!data || data.isErr !== false) &&  response.data.message) {
								uni.showToast({
									title: response.data.message,
									icon: 'none',
									duration: 2000
								});
							}
							if (response.statusCode == 401){
								store.commit('setToken',null)
								// uni.removeStorageSync('TZ_token')
								store.commit('setUser',null)
								// uni.removeStorageSync('TZ_userInfo')
								setTimeout(()=>{
									uni.reLaunch({
										url: '/pagesLogin/index'
									})
								},2000)
							}	
						}
					} else {
						reject(response.data)
						showError(response.data);
					}
				},
				fail: (error) => {
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				}
			});
		})

	},
	post(url, data) {
		return new Promise((resolve, reject) => {
			// data.at_time = new Date().getTime() 
			uni.request({
				url: global.baseUrl + url,
				data: data,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
					"Authorization": uni.getStorageSync('TZ_token') ? 'bearer' + uni.getStorageSync(
						'TZ_token') : null
				},
				method: "POST",
				success: (response) => {
					if (response.data) {
						if (response.statusCode >= 200 && response.statusCode <= 400) {
							resolve(response.data)
						} else {
							reject(response.data)
							if ((!data || data.isErr !== false) &&  response.data.message) {
								uni.showToast({
									title: response.data.message,
									icon: 'none',
									duration: 2000
								});
							}
							if (response.statusCode == 401){
								store.commit('setToken',null)
								// uni.removeStorageSync('TZ_token')
								store.commit('setUser',null)
								// uni.removeStorageSync('TZ_userInfo')
								uni.reLaunch({
									url: '/pages/login/index'
								})
							}
								
						}
					} else {
						reject(response.data)
						showError(response.data);
					}
				},
				fail: (error) => {
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				},
			});
		})
	},
}
const showError = error => {
	let errorMsg = ''
	switch (error.code) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 301:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = '请求地址不存在'
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = "请求失败"
			break
	}
	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 3000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 3000);
		}
	});
}

export default request
